// pages/allssembledetails/allssembledetails.js
var utils = require('../../utils/util.js');
var start_time = ""; //开始时间 需要重新定义  2019-01-23
var end_time = ""; //结束时间 需要重新定义 2019-01-25
var now_time = '';

function getTimeStr(maxtime) {
    var result = '';
    if (maxtime == undefined) {
        maxtime = 0;
    }
    var second = parseInt(maxtime); // 秒
    var min = 0; // 分
    var hour = 0; // 小时
    if (second > 60) {
        min = parseInt(second / 60);
        second = parseInt(second % 60);
        if (min > 60) {
            hour = parseInt(min / 60);
            min = parseInt(min % 60);
        }
    }
    var result = "" + parseInt(second);
    if (min > 0) {
        result = "" + parseInt(min) + ":" + result;
    }
    if (hour > 0) {
        result = "" + parseInt(hour) + ":" + result;
    }
    return result;
}

function CountDown() {
    var result = '';
    now_time = Date.parse(new Date()) / 1000;
    // 活动未开始 

    if (parseInt(now_time) - parseInt(start_time) < 0) {
        result = "距开始还剩" + getTimeStr(parseInt(start_time) - parseInt(now_time));
        return result;
    } else if (parseInt(now_time) - parseInt(start_time) == 0) {
        return '活动开始';
    } else if (parseInt(now_time) - parseInt(end_time) < 0) {
        result = "距结束还剩" + getTimeStr(parseInt(end_time) - parseInt(now_time));
        return result;
    } else {
        clearInterval(timer);
        return '活动已结束';
    }
}

var timer = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        ishidden:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        console.log(options.id);
        that.groupgoodsDetail(options.id);
    },
    
    groupgoodsDetail: function (id) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/groupgoods/goods_detail", { id:id }, function (res) { 
            utils.hideLoading();
            start_time = res.datas.start_time; //开始时间 需要重新定义  2019-01-23
            end_time = res.datas.end_time;
            // 开始倒计时
            timer = setInterval(function () {
                that.setData({
                    lastName: CountDown().slice(0, 5),
                    lastTime:CountDown().slice(5,16)
                });
               // console.log(that.data.lastTime)
                // console.log(CountDown().slice(0,5));
                // console.log(CountDown().slice(5,16))
            }, 1000);
            that.setData({
                data: res.datas,
            })
        })
    },
    preventTouchMove() {},
    //跳转商品评价更多页面
    onEvaluateTap: function (e) {
        wx.navigateTo({
            url: '../commoditydetails/moreevaluatelist/moreevaluatelist',
        })
    },
    //立即购买
    buyNowTap: function (e) {
        wx.navigateTo({
            url: '../confirmorder/confirmorder',
        })
    },
    //拼团规则
    onGuiZeTap:function(e){
       var that=this;
       that.setData({
           ishidden: false
       })
    },
    //关闭拼团规则
    onColseGuiZeTap:function(e){
        var that=this;
        that.setData({
            ishidden: true
        })
    }

})